package com.deyuanyun.pic.pipe.util;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.service.IdentificationService;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.apache.log4j.Logger;

import java.util.*;

/**
 * “管道基础数据” 工具类
 *
 * @author huanglw
 * @date  2016-11-17
 *
 * History: [修改人][时间][修改描述]
 */
public class PipeMgrUtil {

    static Logger logger= Logger.getLogger(PipeMgrUtil.class);

    private static Map<String, Identification> identificationHashMap = new LinkedHashMap<String, Identification>();
    private static final String IDENT_DICT_IN_CACHE="APPLICATION.IDENT_DICT_IN_CACHE";

    private static IdentificationService identificationService = SpringAppliction.getBean(IdentificationService.class);

    /**
     * 得到所有的 地面标识
     */
    private static void loadIdentificationAll(){

        logger.info("加载地面标识参数值");
        List<Identification> list = identificationService.queryIdentificationAll();
        identificationHashMap.clear();
        TempStoreDataSupport.removeData(IDENT_DICT_IN_CACHE);
        for (Identification identification : list){
            identificationHashMap.put(identification.getId(), identification);
        }
        TempStoreDataSupport.pushDataf(IDENT_DICT_IN_CACHE, identificationHashMap);
    }

    /**
     * 得到 地面标识的 编号
     *
     * @param id 码表Id
     * @return 如果没有该值，返回null对象
     */
    public static String getIdentificationNumber(String id){

        if (StringUtils.isNullEmpty(id)){
            return null;
        }
        Map<String, Identification> map = TempStoreDataSupport.pullDataf(IDENT_DICT_IN_CACHE);
        if (map == null) {
            loadIdentificationAll();
            map = TempStoreDataSupport.pullDataf(IDENT_DICT_IN_CACHE);
        }
        Identification identification = map==null ? null : map.get(id);
        if (identification != null){
            return identification.getSignNumber();
        }
        loadIdentificationAll();
        identification = identificationHashMap.get(id);
        if (identification != null){
            return identification.getSignNumber();
        }
        return null;
    }

    /**
     * 根据标识编号，查询标识对象
     *
     * @param signNumber 标识编号
     * @return 如果没有该值，返回null对象
     */
    public static List<Identification> queryIdentification(String signNumber){
        return identificationService.queryIdentification(signNumber);
    }

    /**
     * 根据标识编号，查询标识对象
     *
     * @return
     */
    public static List<Identification> queryIdentificationFlag(){
        return identificationService.queryIdentificationFlag();
    }

    /**
     * 根据标识编号，查询标识对象
     *
     * @param signNumber 标识编号
     * @return 如果没有该值，返回null对象
     */
    public static Identification queryIdentificationFlag(String signNumber){
        List<Identification> identificationList = identificationService.queryIdentificationFlag();
        for (Identification identification : identificationList) {
            if (signNumber != null && signNumber.equals(identification.getSignNumber())) {
                return identification;
            }
        }
        return null;
    }


}
